package com.kl.modular.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.kl.common.exception.SystemException;
import com.kl.common.pager.PageParameter;
import com.kl.common.pager.Pager;
import com.kl.common.util.UUIDUtil;
import com.kl.modular.system.dao.bo.SystemAdminBo;
import com.kl.modular.system.dao.mapper.SystemAdminMapper;
import com.kl.modular.system.dao.model.SystemAdmin;
import com.kl.modular.system.dao.model.SystemAdminRole;
import com.kl.modular.system.service.api.ISystemAdminRoleService;
import com.kl.modular.system.service.api.ISystemAdminService;
import com.kl.modular.system.service.constant.SystemConStant;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 用户serviceImpl业务实现
 * @author XRP_ZJB
 * @date 2016-01-15 13:41
 * @(C) XIGNSOFT Copyright 
 */
@Service
public class SystemAdminServiceImpl implements ISystemAdminService {
	
	private static final Logger logger = LoggerFactory.getLogger(SystemAdminServiceImpl.class);
	
	@Autowired
	private SystemAdminMapper systemAdminMapper;
	
	@Autowired
	private ISystemAdminRoleService systemAdminRoleService;
	
	/**
	 * 用户列表分页
	 */
	public Pager<SystemAdmin> getListByBo(SystemAdminBo systemAdminBo, PageParameter pageParameter) {
		pageParameter.setPageBoolean(true);
		List<SystemAdmin> systemAdminList = systemAdminMapper.getListByBoPage(systemAdminBo, pageParameter);
		return new Pager<SystemAdmin>(pageParameter, systemAdminList);
	}
	
	/**
	 * 添加用户
	 */
	@Transactional(rollbackFor = Exception.class)
	public void addSystemAdmin(SystemAdmin systemAdmin ,String[] roles) throws Exception {
		for(int i = 0;i < roles.length;i++){
			String roleCode = roles[i];
			if(StringUtils.isEmpty(roleCode)){
				continue;
			}
			SystemAdminRole adminRole = new SystemAdminRole();
			adminRole.setAdminCode(systemAdmin.getSystemCode());
			adminRole.setArCode(UUIDUtil.getUniquekey());
			adminRole.setRoleCode(roleCode);
			adminRole.setStatus(SystemConStant.SYSTEM_ADMIN_ROLE_NORMAL_STATUS);
			systemAdminRoleService.addSystemAdminRole(adminRole);
		}
		int result = systemAdminMapper.insert(systemAdmin);
		if(result == 0){
			logger.error("添加用户发生错误");
			logger.error("----------------- "+ JSON.toJSONString(systemAdmin) +"------------------");
			throw new SystemException("添加用户发生错误");
		}
	}

	@Transactional(rollbackFor = Exception.class)
	public void updateByCode(SystemAdmin systemAdmin, String[] roles) throws Exception {
		// 删除之前记录
		systemAdminRoleService.updateDeleteByAdminCode(systemAdmin.getSystemCode());
		for(int i = 0;i < roles.length;i++){
			String roleCode = roles[i];
			if(StringUtils.isEmpty(roleCode)){
				continue;
			}
			SystemAdminRole adminRole = new SystemAdminRole();
			adminRole.setAdminCode(systemAdmin.getSystemCode());
			adminRole.setArCode(UUIDUtil.getUniquekey());
			adminRole.setRoleCode(roleCode);
			adminRole.setStatus(SystemConStant.SYSTEM_ADMIN_ROLE_NORMAL_STATUS);
			systemAdminRoleService.addSystemAdminRole(adminRole);
		}
		int result = systemAdminMapper.updateByPrimaryKey(systemAdmin);
		if(result ==0){
			logger.error("更新用户发生错误");
			logger.error("----------------- "+ JSON.toJSONString(systemAdmin) +"------------------");
			throw new SystemException("更新用户发生错误");
		}
	}

	public void deleteByCode(String[] codes) throws Exception {
		int result = systemAdminMapper.deleteByPrimaryKey(codes);
		if(result ==0){
			logger.error("删除用户发生错误");
			logger.error("----------------- "+ JSON.toJSONString(codes) +"------------------");
			throw new SystemException("删除用户发生错误");
		}
	}

	public SystemAdmin getSystemAdminByCode(String code){
		SystemAdmin systemAdmin = systemAdminMapper.selectByPrimaryKey(code);
		return systemAdmin;
	}

	public SystemAdmin getSystemAdminByBo(SystemAdminBo systemAdminBo) {
		return systemAdminMapper.getSystemAdminByBo(systemAdminBo);
	}

	/**
	 * 查询数
	 */
	public int findAdminCountByBo(SystemAdminBo systemAdminBo){
		if(systemAdminBo == null){
			return 0;
		}
		int count = systemAdminMapper.findCountByBo(systemAdminBo);
		return count;
	}

	/**
	 * 修改密码
	 */
	public void updatePwdByCode(SystemAdmin systemAdmin) throws Exception {
		if (systemAdmin == null || StringUtils.isEmpty(systemAdmin.getSystemPassword()) || StringUtils.isEmpty(systemAdmin.getSystemSalt())) {
			throw new SystemException("修改密码失败");
		}
		int result = systemAdminMapper.updatePwdByPrimaryKey(systemAdmin);
		if(result == 0){
			throw new SystemException("修改密码失败");
		}
	}

	/**
	 * 更新用户状态
	 * @param systemCode
	 * @param oldStatus
	 * @param newStatus
	 */
	public void changeAdminStatusByCode(String systemCode, String oldStatus, String newStatus) throws Exception {
		if(StringUtils.isEmpty(systemCode) || StringUtils.isEmpty(oldStatus) ||
				StringUtils.isEmpty(newStatus)){
			throw new SystemException("更新用户状态发生错误");
		}
		int result = systemAdminMapper.updateStatusByCode(systemCode, newStatus, oldStatus);
		if(result == 0){
			throw new SystemException("更新用户状态发生错误,请重试");
		}
	}

}
